var cropper;
var crop_url=configs.crop_url;

function init()
{	
	cropper = new ImageCropper(300, 300, 180, 180);
	cropper.setCanvas("cropper");
	cropper.addPreview("preview180");
	cropper.addPreview("preview100");
	cropper.addPreview("preview50");

	if(!cropper.isAvaiable())
	{
		alert("Sorry, your browser doesn't support FileReader, please use Firefox3.6+ or Chrome10+ to run it.");
	}
}

function selectImage(fileList)
{
	cropper.loadImage(fileList[0]);
}

function rotateImage(e)
{
	switch(e.target.id)
	{
		case "rotateLeftBtn":
			cropper.rotate(-90);
			break;
		case "rotateRightBtn":
			cropper.rotate(90);
			break;
	}
}

function saveImage()
{
	var img_180 = cropper.getCroppedImageData(180, 180);
	var img_100 = cropper.getCroppedImageData(100, 100);
	var img_50 = cropper.getCroppedImageData(50, 50);
	
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function(e)
	{
		if(xhr.readyState == 4)
		{
			if(xhr.status == 200)
			{
				document.getElementById("status").innerHTML = "<font color='#f00'>上传成功！</font>";
			}
		}
	};
	
	xhr.open("POST", crop_url, true);
	var data = new FormData();
	data.append("username", "flashlizi");
	data.append("size", 180);
	data.append("avatar[180]", img_180);
	data.append("avatar[100]", img_100);
	data.append("avatar[50]", img_50);
	xhr.send(data);
//	console.log(imgData);
}

function trace()
{
	if(typeof(console) != "undefined") console.log(Array.prototype.slice.apply(arguments).join(" "));
};
